package khl.ooo.ui;

import java.awt.TextArea;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;

import khl.ooo.db.OpleidingDb;
import khl.ooo.db.VakDb;
import khl.ooo.domain.opleiding.Opleiding;
import khl.ooo.domain.vak.Vak;

public class Driver {

	/**
	 * TODO commentaar: te lang, opsplitsen
	 */
	public static void main(String[] args) {
		OpleidingDb opldb = new OpleidingDb();
		VakDb vakdb = new VakDb();
		/* HashMap<String, Opleiding >opleidingen = opl.getOpleidingen();
		for (Iterator it = opleidingen.entrySet().iterator(); it.hasNext(); ) {
	            Map.Entry entry = (Map.Entry)it.next();
	            Object key = entry.getKey();
	            Object value = entry.getValue();
	            System.out.println(value.toString());
	    } */
		
		JDialog.setDefaultLookAndFeelDecorated(true);
		boolean ok = true;
		while(ok) {
	        Object[] selectionValues = {"Lijst vakken opvragen", 
	        							"Details vak opvragen", 
	        							"Nieuw vak registreren", 
	        							"Lijst cursussen opvragen",
	        							"Nieuwe cursus registreren",
	        							"Lijst opleidingen opvragen",
	        							"Details opleiding opvragen",
	        							"Nieuwe opleiding registreren",
	        							"Stoppen" };
	        String initialSelection = "Lijst vakken opvragen";
	        Object selection = JOptionPane.showInputDialog(null,"Maak uw keuze:",
		            "KHL Hogeschool", JOptionPane.QUESTION_MESSAGE, null, selectionValues, initialSelection);
	        if (selection != null){ // user duwt op CANCEL
	        	if(selection.equals("Lijst vakken opvragen")) {
	        		boolean tempok = true;
	        		while(tempok) {
	        			String oplCode = JOptionPane.showInputDialog("Opleidingscode:");
	        			if(oplCode != null) {
	        				JFrame frame = new JFrame("Vakken van " + oplCode);
    			            JList list = new JList();
        					TextArea ta1 = new TextArea(); // naamgeving
    			            
	        				try {// vragen a.vakDB
	        					HashMap<String, Vak> vakken = opldb.getVakken(oplCode);
	    			            /*String[] listitems = new String[vakken.size()];
	    			            int i = 0;
	    			            
		        				Map sortedMap = new TreeMap(vakken);
		        				for (Iterator it = sortedMap.entrySet().iterator(); it.hasNext(); ) {
	        			            Map.Entry entry = (Map.Entry)it.next();
	        			            Vak value = (Vak) entry.getValue();
	        			            listitems[i++] = value.getNaam() + " (" + value.getCode() + ")";
		        				}
		        				
		        				list.setListData(listitems);
	    			            frame.add(list);
	    			            frame.pack();
	    			            frame.setLocationRelativeTo(null);
	    			            frame.setVisible(true);
	    			            tempok = false;
	    			            */
	        					Map sortedMap = new TreeMap(vakken);
		        				for (Iterator it = sortedMap.entrySet().iterator(); it.hasNext(); ) {
	        			            Map.Entry entry = (Map.Entry)it.next();
	        			            Vak value = (Vak) entry.getValue();
	        			            ta1.append(value.getNaam() + " (" + value.getCode() + ")\n");
		        				}
	        					
		        				frame.add(ta1);
	    			            frame.pack();
	    			            frame.setLocation(100, 100);
	    			            frame.setVisible(true);
	    			            tempok = false;
	        				} catch (IllegalArgumentException e) {
								JOptionPane.showMessageDialog(null, "Foutieve opleidingscode");
							}
	        			} else {
	        				tempok = false;
	        			}
	        		}
	        	} else if(selection.equals("Details vak opvragen")) {
	        		boolean tempok = true;
	        		while(tempok) {
	        			String vakCode = JOptionPane.showInputDialog("Vakcode:");
	        			if(vakCode != null) {
	        				JFrame frame = new JFrame("Details van " + vakCode);
        					TextArea ta1 = new TextArea();
        					
	        				try {
	        					ta1.append(vakdb.getVak(vakCode).toString());
	        					frame.add(ta1);
	    			            frame.pack();
	    			            frame.setLocation(100, 305);
	    			            frame.setVisible(true);
	    			            tempok = false;
	        				} catch (IllegalArgumentException e) {
								JOptionPane.showMessageDialog(null, "Foutieve vakcode");
							}
	        			} else {
	        				tempok = false;
	        			}
	        		}
	        	} else if(selection.equals("Nieuw vak registreren")) {

	        	} else if(selection.equals("Lijst cursussen opvragen")) {

	        	} else if(selection.equals("Nieuwe cursus registreren")) {

	        	} else if(selection.equals("Lijst opleidingen opvragen")) {
    				JFrame frame = new JFrame("Opleidingen KHL");
		            JList list = new JList();
					TextArea ta1 = new TextArea();
    				HashMap<String, Opleiding> opleidingen = opldb.getOpleidingen();
    					Map sortedMap = new TreeMap(opleidingen);
        				for (Iterator it = sortedMap.entrySet().iterator(); it.hasNext(); ) {
    			            Map.Entry entry = (Map.Entry)it.next();
    			            Opleiding value = (Opleiding) entry.getValue();
    			            ta1.append(value.getCode() + " - " + value.getNaam() + "\n");//toString() in opleiding
        				}
    				frame.add(ta1);
		            frame.pack();
		            frame.setLocation(100, 100);
		            frame.setVisible(true);
	        	} else if(selection.equals("Details opleiding opvragen")) {
	        		boolean tempok = true;
	        		while(tempok) {
	        			String oplCode = JOptionPane.showInputDialog("Opleidingcode:");
	        			if(oplCode != null) {
	        				JFrame frame = new JFrame("Details van " + oplCode);
        					TextArea ta1 = new TextArea();
        					
	        				try {
	        					ta1.append(opldb.getOpleidingen().get(oplCode).toString());
	        					HashMap<String, Vak> vakken = opldb.getVakken(oplCode);
	        					Map sortedMap = new TreeMap(vakken);
	        					ta1.append("\nVakken:\n");
		        				for (Iterator it = sortedMap.entrySet().iterator(); it.hasNext(); ) {
	        			            Map.Entry entry = (Map.Entry)it.next();
	        			            Vak value = (Vak) entry.getValue();
	        			            ta1.append(value.getNaam() + " (" + value.getCode() + ")\n");
		        				}
	        					
	        					frame.add(ta1);
	    			            frame.pack();
	    			            frame.setLocation(100, 305);
	    			            frame.setVisible(true);
	    			            tempok = false;
	        				} catch (IllegalArgumentException e) {
								JOptionPane.showMessageDialog(null, "Foutieve opleidingcode");
							}
	        			} else {
	        				tempok = false;
	        			}
	        		}
	        	} else if(selection.equals("Nieuwe opleiding registreren")) {
	        		
	        	} else {
	        		ok = false;
	        	}
	        } else {
	        	ok = false;
	        }
		}
	}
}

//TODO eindcommentaar: veel herhaling van code.